package com.whimscrew.user.controller;

import brave.Span;
import brave.SpanCustomizer;
import brave.Tracer;
import com.whimscrew.user.model.User;
import com.whimscrew.user.service.DeptService;
import com.whimscrew.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.annotation.NewSpan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("v1/user")
public class UserController {


    @Autowired
    UserService userService;

    @Autowired
    DeptService deptService;

    @Autowired
    Tracer tracer;


    @PostMapping
    public User saveUser(String userName) throws InterruptedException {
        return userService.save(userName);
    }

    @GetMapping("dept")
    public String userDept( )   {
        //自定义跨度，模拟发送请求到第三方插件校验
        customSpan();
        return  deptService.getDept();
    }


    /**
     * 自定义跨度
     */
    @NewSpan
    public void customSpan(){
        Span pluginValidate = tracer.nextSpan().start().name("pluginValidate");
        try {
            //发送请求到第三方插件校验
            Thread.sleep(1500);
        }catch (Exception ex){
            ex.printStackTrace();
        } finally {
            pluginValidate.tag("plugin.service", "pluginAPI");
            pluginValidate.finish();
        }
    }

}
